package com.swinboo.im.endpoint;

import com.huang.entity.result.HCollectionResult;
import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResponseEnum;
import com.huang.entity.result.ResultBuilder;
import com.huang.exception.IllegalParamException;
import com.swinboo.im.api.EMContactApi;
import com.swinboo.im.entity.param.AddContactParam;
import com.swinboo.im.entity.param.ContactListParam;
import com.swinboo.im.entity.param.RemoveContactParam;
import com.swinboo.im.service.EMContactService;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Swinboo
 * @date 2022-03-19 20:05
 * @description:
 */

@RestController
public class EMContactEndpoint implements EMContactApi {
    @Resource
    private EMContactService emContactService;

    @Override
    public HStringResult<Boolean> add(AddContactParam addContactParam) {
        try {
            addContactParam.check();
            return emContactService.add(addContactParam);
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSingle(e.getMessage());
        } catch (Exception e) {
            return ResultBuilder.failHSingle(e.getMessage());
        }
    }

    @Override
    public HStringResult<Boolean> remove(RemoveContactParam removeContactParam) {
        try {
            removeContactParam.check();
            return emContactService.remove(removeContactParam);
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSingle(e.getMessage());
        } catch (Exception e) {
            return ResultBuilder.failHSingle(e.getMessage());
        }
    }

    @Override
    public HCollectionResult<String> list(ContactListParam contactListParam) {
        try {
            contactListParam.check();
            return emContactService.list(contactListParam);
        } catch (IllegalParamException e) {
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        } catch (Exception e) {
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        }
    }
}
